/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.model;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import javax.jdo.annotations.*;

/**
 *
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class SourceLocation extends ModelObject {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key id;

    @Persistent
    private ApplicationVersion applicationVersion;
    @Persistent
    private String fileLine;
    @Persistent
    private String fileName;
    @Persistent
    private int lineNumber;
    @Persistent
    private String className;
    @Persistent
    private String methodName;


    public SourceLocation() {
    }

    public SourceLocation(ApplicationVersion applicationVersion, String fileName, int lineNumber, String className,
                          String methodName) {
        this.applicationVersion = applicationVersion;
        this.fileLine = getFileLine(fileName, lineNumber);
        this.fileName = fileName;
        this.lineNumber = lineNumber;
        this.className = className;
        this.methodName = methodName;
    }

    public static String getHashKey(ApplicationVersion applicationVersion,
                                    String fileName, int lineNumber, String className, String methodName) {
        return getHashKey(KeyFactory.keyToString(applicationVersion.getId()), fileName, lineNumber, className,
                methodName);

    }
    public static String getHashKey(String applicationVersionId,
                                    String fileName, int lineNumber, String className, String methodName) {
        StringBuilder str = new StringBuilder(applicationVersionId);
        str.append(':');
        str.append(fileName);
        str.append(':');
        str.append(lineNumber);
        str.append(':');
        str.append(className);
        str.append(':');
        str.append(methodName);
        return str.toString();
    }

    public String getHashKey() {
        return getHashKey(applicationVersion, fileName, lineNumber, className, methodName);
    }

    public String toString() {
        StringBuilder str = new StringBuilder();
        str.append(className);
        str.append('.');
        str.append(methodName);
        str.append('(');
        str.append(fileName);
        str.append(':');
        str.append(lineNumber);
        str.append(')');
        return str.toString();
    }

    @Override
    public int hashCode() {
        return getHashKey().hashCode();
    }

    public Key getId() {
        return id;
    }

    public ApplicationVersion getApplicationVersion() {
        return applicationVersion;
    }

    public void setApplicationVersion(ApplicationVersion applicationVersion) {
        this.applicationVersion = applicationVersion;
    }

    public static String getFileLine(String fileName, int lineNumber) {
        return fileName + ":" + lineNumber;
    }

    public String getFileLine() {
        return fileLine;
    }

    public void setFileLine(String fileLine) {
        this.fileLine = fileLine;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public int getLineNumber() {
        return lineNumber;
    }

    public void setLineNumber(int lineNumber) {
        this.lineNumber = lineNumber;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }
}
